package edu.tongji.jiaowu.webapp.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.tongji.quickCRUDQ.define.Column;
import com.tongji.quickCRUDQ.model.CommonBean;
import com.tongji.quickCRUDQ.util.JsonDataSet;
import com.tongji.quickCRUDQ.util.RequestUtil;
import com.tongji.quickCRUDQ.util.StringUtil;

import edu.tongji.jiaowu.service.ExternalExamStudentsManager;
import edu.tongji.jiaowu.service.ExternalExamTypeManager;
import edu.tongji.jiaowu.service.ExternalScoreManager;
import edu.tongji.jiaowu.service.ParameterManager;
import edu.tongji.jiaowu.service.StudentManager;
import edu.tongji.jiaowu.service.VcodeManager;
import edu.tongji.jiaowu.webapp.session.SessionParameter;
import edu.tongji.jiaowu.webapp.session.SessionParameterLocal;


public class ExamEntryController extends MultiActionController {
	private VcodeManager vcodeManager;
	private ParameterManager parameterManager;
	private ExternalExamTypeManager externalExamTypeManager;
	private ExternalExamStudentsManager externalExamStudentsManager;
	private StudentManager studentManager;
	private ExternalScoreManager externalScoreManager;
	
	public ModelAndView parameter(HttpServletRequest request, HttpServletResponse response){
		Map model=new HashMap();
		SessionParameter sp=SessionParameterLocal.getSessionParameter();
		String currentYear=sp.getCurrentSchoolYear();
		String currentSemester=sp.getCurrentSchoolSemester();
		model.put("currentYear", currentYear);
		model.put("currentSemester", currentSemester);
		CommonBean years=vcodeManager.findCodes("SCHOOL_YEAR");
		model.put("years", years.getData());
		
		CommonBean confictTypes=null;
		String cmd=request.getParameter("cmd");
//报名开关		
		if ("EXTERNAL_EXAM_ENTRY_SWITCH".equals(cmd)) {
			String entrySwitch=request.getParameter("EXTERNAL_EXAM_ENTRY_SWITCH");
			externalExamTypeManager.updateSwitch(entrySwitch,currentYear,currentSemester);
		} else
//当前学年度学期有效报名类型			
		if ("EXTERNAL_EXAM_ENTRY_TYPES".equals(cmd)) {
			String[] entryTypes=request.getParameterValues("entryType");
			//TODO mytodo 控制器中存在事务管理问题
			vcodeManager.status("EXTERNAL_EXAM_TYPE",null,"N");
			if (entryTypes!=null) {
				vcodeManager.status("EXTERNAL_EXAM_TYPE",entryTypes,"Y");
			}
		} else
//选择当前报名类型			
		if ("EXTERNAL_EXAM_ENTRY_CURRENT_TYPE".equals(cmd)) {
			String currentType = request.getParameter("EXTERNAL_EXAM_ENTRY_CURRENT_TYPE");
			model.put("currentType", currentType);
			confictTypes = readCurrentType(currentYear, currentSemester, currentType, confictTypes, model);
		}else
//设置当前报名类型			
		if ("EXTERNAL_EXAM_ENTRY_CURRENT_TYPE_PARAM".equals(cmd)) {
			String currentType = request.getParameter("EXTERNAL_EXAM_ENTRY_CURRENT_TYPE");
			String currentTypeName = request.getParameter("currentTypeName");
			String currentEntryStatus = request.getParameter("currentEntryStatus");
			String[] conflictTypes=request.getParameterValues("conflictType");
			String conflictTypeStr="";
			for (int i = 0; conflictTypes!=null&&i < conflictTypes.length; i++) {
				conflictTypeStr+=conflictTypes[i];
				if (i<conflictTypes.length-1) {
					conflictTypeStr+=",";
				}
			}
			CommonBean externalExamTypeBean=externalExamTypeManager.createExternalExamType();
			externalExamTypeBean.push("schoolYear", currentYear);
			externalExamTypeBean.push("schoolSemester", currentSemester);
			externalExamTypeBean.push("examType", currentType);
			externalExamTypeBean.push("examTypeName", currentTypeName);
			externalExamTypeBean.push("entryStatus", currentEntryStatus);
			externalExamTypeBean.push("conflictType",conflictTypeStr);
			externalExamTypeManager.reset(externalExamTypeBean);

			model.put("currentType", currentType);
			confictTypes = readCurrentType(currentYear, currentSemester, currentType, confictTypes, model);
		}
		
		String entrySwitch=parameterManager.findParameterValue("EXTERNAL_EXAM_ENTRY_SWITCH");
		model.put("EXTERNAL_EXAM_ENTRY_SWITCH", entrySwitch);
		CommonBean types=vcodeManager.findAllCodes("EXTERNAL_EXAM_TYPE");
		if (confictTypes!=null) {//当前报名类型有冲突类型
			Column conflictStatusColumn = Column.createColumn("CONFLICT_STATUS", "conflictStatus", Column.DATA_TYPE_VARCHAR, "冲突状态", null, null);
			types.addColumn(conflictStatusColumn);//
			for (int i = 0; i < types.getSize(); i++) {
				String code1=types.getPropertyAsString(i, "code");
				types.setProperty(i, "conflictStatus", "0");//默认为不冲突
				for (int j = 0; j < confictTypes.getSize(); j++) {
					String code2=confictTypes.getPropertyAsString(j, "code");
					if (code1.equals(code2)) {
						types.setProperty(i, "conflictStatus", "1");//冲突
					}
				}
			}
		}
		model.put("types", types.getData());
		return new ModelAndView("examEntry/parameter","model",model);
	}

	private CommonBean readCurrentType(String currentYear, String currentSemester, String currentType, CommonBean confictTypes, Map model) {
		CommonBean typeDetail = externalExamTypeManager.findExternalExamTypeByCode(currentYear, currentSemester, currentType);
		if (typeDetail.getSize()==1) {
			model.put("entryStatus", typeDetail.getPropertyAsString(0,"entryStatus"));
			String conflictTypeStr=typeDetail.getPropertyAsString(0, "conflictType");
			confictTypes=vcodeManager.findCodes("EXTERNAL_EXAM_TYPE",conflictTypeStr);
		}
		return confictTypes;
	}

	public ModelAndView students(HttpServletRequest request, HttpServletResponse response){
		Map model=new HashMap();
		SessionParameter sp=SessionParameterLocal.getSessionParameter();
		String currentYear=sp.getCurrentSchoolYear();
		String currentSemester=sp.getCurrentSchoolSemester();
		model.put("currentYear", currentYear);
		model.put("currentSemester", currentSemester);
		CommonBean schoolYear=vcodeManager.findCodes("SCHOOL_YEAR");
		model.put("schoolYear", JsonDataSet.listToJson(schoolYear.getData()));
		CommonBean schoolSemester=vcodeManager.findCodes("SCHOOL_SEMESTER");
		model.put("schoolSemester", JsonDataSet.listToJson(schoolSemester.getData()));
		CommonBean examTypes=vcodeManager.findCodes("EXTERNAL_EXAM_TYPE");
		model.put("externalExamTypes", JsonDataSet.listToJson(examTypes.getData()));
		return new ModelAndView("examEntry/students","model",model);
	}
	
	public ModelAndView statistics(HttpServletRequest request, HttpServletResponse response){
		Map model=new HashMap();
		SessionParameter sp=SessionParameterLocal.getSessionParameter();
		String currentYear=sp.getCurrentSchoolYear();
		String currentSemester=sp.getCurrentSchoolSemester();
		model.put("currentYear", currentYear);
		model.put("currentSemester", currentSemester);
		CommonBean years=vcodeManager.findCodes("SCHOOL_YEAR");
		model.put("years", years.getData());
		CommonBean examTypes=vcodeManager.findCodes("EXTERNAL_EXAM_TYPE");
		model.put("examTypes", examTypes.getData());
		String currentType=StringUtil.NVL(request.getParameter("currentType"), "");
		String currentDepartment=StringUtil.NVL(request.getParameter("currentDepartment"), "");
		String currentMajor=StringUtil.NVL(request.getParameter("currentMajor"), "");
		model.put("currentType", currentType);
		model.put("currentDepartment", currentDepartment);
		model.put("currentMajor", currentMajor);
		List departments=externalExamStudentsManager.entryDepartments(currentYear,currentSemester,currentType);
		model.put("departments", departments);
		List majors=externalExamStudentsManager.entryMajors(currentYear,currentSemester,currentType,currentDepartment);
		model.put("majors", majors);
		
		String cmd=request.getParameter("cmd");
		if ("STATISTICS_DETAIL".equals(cmd)) {
			List statisticsData=externalExamStudentsManager.statisticsDestail(currentYear,currentSemester,currentType,currentDepartment,currentMajor);
			if (statisticsData.size()>0) {
				model.put("statisticsData", statisticsData);
			}
		}
		
		return new ModelAndView("examEntry/statistics","model",model);
	}
	
	public ModelAndView downloadImages(HttpServletRequest request, HttpServletResponse response) throws IOException {
		SessionParameter sp=SessionParameterLocal.getSessionParameter();
		String currentYear=sp.getCurrentSchoolYear();
		String currentSemester=sp.getCurrentSchoolSemester();
		String examType=request.getParameter("examType");
		List list=externalExamStudentsManager.findStudentList(currentYear, currentSemester, examType);
		request.setAttribute("fnames", list);
		String path = request.getSession().getServletContext().getRealPath("/WEB-INF/resources/studentPhotos");
		request.setAttribute("path", path);
		return new ModelAndView("forward:/common/downloadImages.html");
	}
	
	
	public ModelAndView entry(HttpServletRequest request, HttpServletResponse response){
		Map model=new HashMap();
		SessionParameter sp=SessionParameterLocal.getSessionParameter();
		String currentYear=sp.getCurrentSchoolYear();
		String currentSemester=sp.getCurrentSchoolSemester();
		String studentNo=request.getParameter("studentNo");
		if (studentNo==null) {
			studentNo=sp.getUserAccount();
		}
		
		String cmd=request.getParameter("cmd");
		if (cmd!=null) {
			model.put("note_div_display","none");
			model.put("entry_div_display","block");
		}else{
			model.put("note_div_display","block");
			model.put("entry_div_display","none");
		}
		if ("STUDENT_ENTRY".equals(cmd)) {
			String entryType=request.getParameter("entryType");
			boolean isPass=true;
			if ("02".equals(entryType)) {//全国大学英语六级考试
				String cet4="01";//全国大学英语四级考试
				isPass=externalScoreManager.check(cet4,studentNo);
			}
			if (isPass) {
				CommonBean student=externalExamStudentsManager.createExternalExamStudents();
				student.push("schoolYear", currentYear);
				student.push("schoolSemester", currentSemester);
				student.push("examType", entryType);
				student.push("studentNo", studentNo);
				student.push("status", "N");//未付费
				externalExamStudentsManager.insertExternalExamStudents(student);
			} else {
				model.put("error", "未通过英语四级,不能报考英语六级!");
			}
		}else 
		if ("CANCEL_ENTRY".equals(cmd)) {
			String id=request.getParameter("ID");
			externalExamStudentsManager.remove(id);
		}	
		
		CommonBean entryStudent=externalExamStudentsManager.findExternalExamStudentsByStudentNo(studentNo, currentYear, currentSemester);
		model.put("entryTypes", entryStudent.getData());
		
		String entrySwitch=parameterManager.findParameterValue("EXTERNAL_EXAM_ENTRY_SWITCH");
		model.put("entrySwitch", entrySwitch);
//		boolean arrears=studentManager.getArrears(studentNo);
//		if (arrears) {
//			model.put("arrears", "Y");
//			return new ModelAndView("examEntry/entry","model",model);
//		}
		if ("N".equals(entrySwitch)) {//报名开关已关闭
			return new ModelAndView("examEntry/entry","model",model);
		}

		CommonBean student=studentManager.findStudentByNo(studentNo);
		model.put("studentNo", studentNo);
		model.put("studentName", student.pop("studentName"));
		model.put("majorName", student.pop("majorName"));
		model.put("grade", student.pop("grade"));
		model.put("identityNo", student.pop("identityNo"));
		
		CommonBean permitTypes=externalExamTypeManager.findPermitTypes(currentYear,currentSemester,studentNo);
		model.put("currentYear", currentYear+"-"+(Integer.parseInt(currentYear)+1));
		model.put("currentSemester", currentSemester);
		model.put("permitTypes", permitTypes.getData());
		return new ModelAndView("examEntry/entry","model",model);
	}

	public VcodeManager getVcodeManager() {
		return vcodeManager;
	}

	public void setVcodeManager(VcodeManager vcodeManager) {
		this.vcodeManager = vcodeManager;
	}

	public ParameterManager getParameterManager() {
		return parameterManager;
	}

	public void setParameterManager(ParameterManager parameterManager) {
		this.parameterManager = parameterManager;
	}

	public ExternalExamTypeManager getExternalExamTypeManager() {
		return externalExamTypeManager;
	}

	public void setExternalExamTypeManager(ExternalExamTypeManager externalExamTypeManager) {
		this.externalExamTypeManager = externalExamTypeManager;
	}

	public StudentManager getStudentManager() {
		return studentManager;
	}

	public void setStudentManager(StudentManager studentManager) {
		this.studentManager = studentManager;
	}

	public ExternalExamStudentsManager getExternalExamStudentsManager() {
		return externalExamStudentsManager;
	}

	public void setExternalExamStudentsManager(ExternalExamStudentsManager externalExamStudentsManager) {
		this.externalExamStudentsManager = externalExamStudentsManager;
	}

	public ExternalScoreManager getExternalScoreManager() {
		return externalScoreManager;
	}

	public void setExternalScoreManager(ExternalScoreManager externalScoreManager) {
		this.externalScoreManager = externalScoreManager;
	}
	

}
